Creating Timer at Runtime in C Sharp .NET
Here I’m going to create a timer at runtime.
I’ll set timer’s interval to 5seconds = 5000 milliseconds.
And after 5sec back color of the form will be changed to blue.
Code
//creating timer
Timer tm = new Timer();
//at button click event setting timer interval to 5000 and enabling the timer
private void btnClick_Click(object sender, EventArgs e)
{
tm.Interval = 5000;
tm.Enabled = true;
}
//writing timer event handler at tick event of timer.
private void tm_Tick(object sender, EventArgs e)
{
this.BackColor = Color.Blue;
tm.Enabled = false;
}
//at form load event creating timer event handler
private void Timer2_Load(object sender, EventArgs e)
{
tm.Tick += new EventHandler(tm_Tick);
}
Screenshot
The back color of form changed after 5 seconds of a button click.
You should read this Article - Dynamically loading an image in Image control in ASP.NET
Anonymous User
18-Feb-2019Good Article.
Sushant Mishra
13-Jul-2017Thanks for sharing informative post.